package com.atguigu.assionexcel.UnSafeCollection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 问题：List<String> list = new ArrayList(); 多线程下出现ConcurrentModificationException异常
 * 出现原因：add 方法没有加锁
 * 解决方法：换一个集合类
 *        1、List<String> list = new Vector<>();  JDK1.0 就存在了！
 *        2、List<String> list = Collections.synchronizedList(new ArrayList<>());
 *        3、List<String> list = new CopyOnWriteArrayList<>();
 */
public class ListT1 {
    public static void main(String[] args) {
        List list = new CopyOnWriteArrayList<>();   //推荐
        for (int i = 0; i < 30000; i++) {
            new Thread(() -> {
               list.add(UUID.randomUUID().toString().substring(0,3));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }
}
